function loadItemsNumber(){
    var items_number = 0;
    var cart_products = getcookie("cart_products_" + id_store);
    if(cart_products != false){
        cart_products = jQuery.parseJSON(cart_products);
    }
    for (var index in cart_products){
        items_number += parseInt(cart_products[index]['quantity'], 10);
    }
    $('#shopping_cart').html('<span>' + items_number + "</span>");   
}
function displayMessage(items_number){
    var message = msg_add_cart_successfull_n_items;
    if(items_number <= 1){
        message = msg_add_cart_successfull_n_item;
    }

    message = message.replace(/\%s/, items_number);
    message = message.replace(/\#/, shopping_cart_link);
    $(".msg_panel").remove();
    var panel_message = '<div class="msg_panel"><p>' + message + '</p></div>';
    $('.product_detail').prepend(panel_message);
}
function addToCart(id_product){
    var cart_products = getcookie("cart_products_" + id_store);
    var add_product = {id: id_product, quantity: 1};
    var items_number = 0;

    if(cart_products == false)
    {
        cart_products = {};
        cart_products[id_product] = add_product;
        setcookie("cart_products_" + id_store, JSON.stringify(cart_products),time_expired,cookie_path);
    }
    else
    {
        cart_products = jQuery.parseJSON(cart_products);
        if(cart_products[id_product] == undefined)
        {
            cart_products[id_product] = add_product;
        }
        else{
            cart_products[id_product]['quantity']++;
        }
        setcookie("cart_products_" + id_store, JSON.stringify(cart_products),time_expired,cookie_path);
    }
    for (var index in cart_products){
        items_number += parseInt(cart_products[index]['quantity'], 10);
    }
    $('#shopping_cart').html('<span>' + items_number + "</span> ");
    //displayMessage(items_number);
    setcookie("is_add_to_cart_" + id_store, "1",time_expired,cookie_path);
    top.location.href = shopping_cart_link;
}

function formatCurrency(number){
    var format = {
        numberOfDecimals:   format_currency['decimals'],
        decimalSeparator:   format_currency['decimal'],
        thousandSeparator:  format_currency['thousand']
    }
    if((number - Math.floor(number)) == 0)
    {
        format['numberOfDecimals'] = 0;
    }
    return $().number_format(number, format) + " " + currency;
}

function loadShoppingCart(){
    var total_payment = 0;
    var no_row = 0;
    $(".cart_product_row").each(function(){
        var price = $(this).find('.price').attr("value");
        var quantity = $(this).find('.quantity').val();
        if(parseInt(quantity, 10) > 0){
            quantity = parseInt(quantity, 10);
        } else {
            quantity = 1;
        }
        $(this).find('.quantity').val(quantity);
        
        if(price != 'N/A') {
            $(this).find('.price').text(formatCurrency(price));
            var payment = price * quantity;
            total_payment += payment;
            $(this).find('.payment span').text(formatCurrency(payment));
        } else {
            $(this).find('.price').text('N/A');
            $(this).find('.payment span').text('N/A');
        }
        no_row++;
    });
    if(no_row == 0){
        $("#empty_cart_row").removeAttr("style");
    }else{
        $("#empty_cart_row").css("display", "none");
    }

    $(".total_payment span").text(formatCurrency(total_payment));
}
function updateShoppingCart(){
    var cart_products = getcookie("cart_products_" + id_store);
    var msg_successful = '<div class="msg_successful"><p>' + msg_update_shopping_cart_successful + '</p></div>';
    $(".msg_successful").remove();    
    if(cart_products != false){
        cart_products = jQuery.parseJSON(cart_products);

        $(".quantity").each(function(){
            var quantity = $(this).val();
            var id = $(this).attr('id');
            if(parseInt(quantity, 10) > 0){
                cart_products[id]['quantity'] = parseInt(quantity, 10);
            } else {
                cart_products[id]['quantity'] = 1;
            }
        });
        setcookie("cart_products_" + id_store, JSON.stringify(cart_products),'',cookie_path);
        $('#tbl_cart_products').before(msg_successful);
    }
}
function removeProduct(id, obj){
    if(confirm(msg_confirm_remove_cart_product)){
        var cart_products = getcookie("cart_products_" + id_store);
        var msg_successful = '<div class="msg_successful"><p>' + msg_remove_cart_product_successful + '</p></div>';
        $(".msg_successful").remove();    
        if(cart_products != false){
            cart_products = jQuery.parseJSON(cart_products);
            if(cart_products[id] != undefined){
                delete cart_products[id];
                $(obj).remove();
            }

            setcookie("cart_products_" + id_store, JSON.stringify(cart_products),'',cookie_path);
            loadShoppingCart();
            $('#tbl_cart_products').before(msg_successful);
        }
    }
}